//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Codingly.SvnExternalUpdater.Layout;
using Codingly.SvnExternalUpdater.Presenters;
using Codingly.SvnExternalUpdater.Properties;

namespace Codingly.SvnExternalUpdater.Views
{
	public partial class ExternalView : UserControl, IExternalView
	{
		private readonly LayoutManager layoutManager;

		protected SynchronizationContext syncronizationContext;

		public ExternalView(LayoutManager layoutManager, SynchronizationContext syncronizationContext)
		{
			InitializeComponent();
			this.layoutManager = layoutManager;
			this.syncronizationContext = syncronizationContext;
		}

		public void BeginUpdate()
		{
		}

		public void EndUpdate()
		{
		}

		public void SetInfo(string projectName, string revision, string headRevision, bool upToDate)
		{
			this.syncronizationContext.Post(delegate
			                                {
			                                	this.labelProject.Text = projectName;
			                                	this.labelRemoteRevision.Text = headRevision;
			                                	this.labelLocalRevision.Text = revision;
			                                	UpdateStatus(upToDate);
			                                }, null);
		}

		private void UpdateStatus(bool upToDate)
		{
		}

		private void labelCurrentRevision_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			Presenter.ShowLog();
		}

		private void linkLabelUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			Presenter.UpdateExternalRevision();
		}

		#region IExternalView Members

		public ExternalPresenter Presenter { get; set; }

		public void Append(string placeHolderKey)
		{
			this.layoutManager.Append(placeHolderKey, this);
		}

		public void Remove(string placeHolderKey)
		{
			this.layoutManager.Remove(placeHolderKey, this);
		}

		public bool IsUpToDate
		{
			set
			{
				this.syncronizationContext.Post(delegate
				                                {
				                                	if (value)
				                                	{
				                                		this.pictureBox1.Image = Resources.GreenStatus;
				                                		this.labelLocalRevision.ForeColor = Color.DarkSeaGreen;
				                                	}
				                                	else
				                                	{
				                                		this.pictureBox1.Image = Resources.RedStatus;
				                                		this.labelLocalRevision.ForeColor = Color.IndianRed;
				                                	}
				                                	this.linkLabelUpdate.Enabled = !value;
				                                }, null);
			}
		}

		public string LocalRevision
		{
			set { this.syncronizationContext.Post(delegate { this.labelLocalRevision.Text = value; }, null); }
		}

		public string ProjectName
		{
			set { this.syncronizationContext.Post(delegate { this.labelProject.Text = value; }, null); }
		}

		public string RemoteRevision
		{
			set { this.syncronizationContext.Post(delegate { this.labelRemoteRevision.Text = value; }, null); }
		}

		#endregion
	}
}